Modernize Humminbird internals (#1322)
authorRobert Lipe <robertlipe@users.noreply.github.com>
Thu, 15 Aug 2024 05:51:56 +0000 (00:51 -0500)
committerGitHub <noreply@github.com>
Thu, 15 Aug 2024 05:51:56 +0000 (00:51 -0500)
* Modernize Humminbirds internals

---------

Co-authored-by: Robert Lipe <robertlipe@gpsbabel.org>
.gitignore
humminbird.cc

index d0e84069e783300e3bb7d28c0fda92bb5e2fb9c1..eb7338ca291cf3abcc7aace3ff8002e84fc77ab9 100644 (file)
@@ -9,6 +9,7 @@
 /*.gcda
 /*.gcno
 /*.gcov
+/.cmake/
 /.vs/
 /.vscode/
 /GPSBabel
index d4a597b26ff4b5098ec255e40833c57c7dcc7a47..3d66cc0584cc5870c7aad661721244074ebcf666 100644 (file)
@@ -30,7 +30,7 @@
 #include <cstring>              // for strncpy, memcpy, memset
 #include <numbers>              // for inv_pi, pi
 
-#include "defs.h"               // for Waypoint, be_read32, be_read16, be_write32, fatal, xfree, be_write16, route_head, xcalloc, track_add_wpt, xstrndup
+#include "defs.h"               // for Waypoint, be_read32, be_read16, be_write32, fatal, be_write16, route_head, track_add_wpt
 #include "mkshort.h"            // for MakeShort
 #include "src/core/datetime.h"  // for DateTime
 
@@ -229,7 +229,7 @@ HumminbirdBase::humminbird_rd_deinit() const
 void
 HumminbirdBase::humminbird_read_wpt(gbfile* fin)
 {
-  humminbird_waypt_t w;
+  humminbird_waypt_t w{0};
 
   if (! gbfread(&w, 1, sizeof(w), fin)) {
     fatal(MYNAME ": Unexpected end of file!\n");
@@ -247,24 +247,20 @@ HumminbirdBase::humminbird_read_wpt(gbfile* fin)
 
   auto* wpt = new Waypoint;
 
-  // Could probably find a way to eliminate the alloc/copy.
-  char* s = xstrndup(w.name, sizeof(w.name));
-  wpt->shortname = s;
-  xfree(s);
-
+  wpt->shortname = QByteArray(w.name, static_cast<int>(qstrnlen(w.name, sizeof(w.name))));
   wpt->SetCreationTime(w.time);
 
   double guder = gudermannian_i1924(w.north);
   wpt->latitude = geocentric_to_geodetic_hwr(guder);
-  wpt->longitude = (double)w.east / EAST_SCALE * 180.0;
+  wpt->longitude = static_cast<double>(w.east) / EAST_SCALE * 180.0;
 
   wpt->altitude  = 0.0; /* It's from a fishfinder... */
 
   if (w.depth != 0) {
-    wpt->set_depth((double)w.depth / 100.0);
+    wpt->set_depth(static_cast<double>(w.depth) / 100.0);
   }
 
-  int num_icons = sizeof(humminbird_icons) / sizeof(humminbird_icons[0]);
+  int num_icons = std::size(humminbird_icons);
   if (w.icon < num_icons) {
     wpt->icon_descr = humminbird_icons[w.icon];
   }
@@ -297,7 +293,7 @@ void
 HumminbirdBase::humminbird_read_route(gbfile* fin) const
 {
 
-  humminbird_rte_t hrte;
+  humminbird_rte_t hrte{0};
 
   if (! gbfread(&hrte, 1, sizeof(hrte), fin)) {
     fatal(MYNAME ": Unexpected end of file!\n");
@@ -320,11 +316,7 @@ HumminbirdBase::humminbird_read_route(gbfile* fin) const
         if (rte == nullptr) {
           rte = new route_head;
           route_add_head(rte);
-          // TODO: find a way to eliminate the copy.
-          char* s = xstrndup(hrte.name, sizeof(hrte.name));
-          rte->rte_name = s;
-          xfree(s);
-          /* rte->rte_num = hrte.num + 1; only internal number */
+          rte->rte_name = QByteArray(hrte.name, static_cast<int>(qstrnlen(hrte.name, sizeof(hrte.name))));
         }
         route_add_wpt(rte, new Waypoint(*wpt));
       }
@@ -336,7 +328,7 @@ void
 HumminbirdBase::humminbird_read_track(gbfile* fin)
 {
 
-  humminbird_trk_header_t th;
+  humminbird_trk_header_t th{0};
 
   if (! gbfread(&th, 1, sizeof(th), fin)) {
     fatal(MYNAME ": Unexpected end of file reading header!\n");
@@ -369,7 +361,7 @@ HumminbirdBase::humminbird_read_track(gbfile* fin)
   /* num_points is actually one too big, because it includes the value in
      the header. But we want the extra point at the end because the
      freak-value filter below looks at points[i+1] */
-  auto* points = (humminbird_trk_point_t*) xcalloc(th.num_points, sizeof(humminbird_trk_point_t));
+  auto* points = new humminbird_trk_point_t[th.num_points]();
   if (! gbfread(points, sizeof(humminbird_trk_point_t), th.num_points-1, fin)) {
     fatal(MYNAME ": Unexpected end of file reading points!\n");
   }
@@ -380,10 +372,7 @@ HumminbirdBase::humminbird_read_track(gbfile* fin)
   auto* trk = new route_head;
   track_add_head(trk);
 
-  // TODO: find a way to eliminate the copy.
-  char* s = xstrndup(th.name, sizeof(th.name));
-  trk->rte_name = s;
-  xfree(s);
+  trk->rte_name = QByteArray(th.name, static_cast<int>(qstrnlen(th.name, sizeof(th.name))));
   trk->rte_num  = th.trk_num;
 
   /* We create one wpt for the info in the header */
@@ -396,7 +385,7 @@ HumminbirdBase::humminbird_read_track(gbfile* fin)
   /* No depth info in the header. */
   track_add_wpt(trk, first_wpt);
 
-  for (int i = 0 ; i<th.num_points-1 ; i++) {
+  for (int i = 0 ; i < th.num_points-1 ; i++) {
     auto* wpt = new Waypoint;
 
     points[i].depth      = be_read16(&points[i].depth);
@@ -428,7 +417,7 @@ HumminbirdBase::humminbird_read_track(gbfile* fin)
     wpt->altitude  = 0.0;
 
     if (points[i].depth != 0) {
-      wpt->set_depth((double)points[i].depth / 100.0);
+      wpt->set_depth(static_cast<double>(points[i].depth) / 100.0);
     }
 
     if (i == th.num_points-2 && th.time != 0) {
@@ -440,14 +429,14 @@ HumminbirdBase::humminbird_read_track(gbfile* fin)
     }
     track_add_wpt(trk, wpt);
   }
-  xfree(points);
+  delete[] points;
 }
 
 void
 HumminbirdBase::humminbird_read_track_old(gbfile* fin)
 {
 
-  humminbird_trk_header_old_t th;
+  humminbird_trk_header_old_t th{0};
   constexpr int file_len = 8048;
   char namebuf[TRK_NAME_LEN];
 
@@ -476,7 +465,7 @@ HumminbirdBase::humminbird_read_track_old(gbfile* fin)
   /* num_points is actually one too big, because it includes the value in
      the header. But we want the extra point at the end because the
      freak-value filter below looks at points[i+1] */
-  auto* points = (humminbird_trk_point_old_t*)xcalloc(th.num_points, sizeof(humminbird_trk_point_old_t));
+  auto* points = new humminbird_trk_point_old_t[th.num_points]();
   if (! gbfread(points, sizeof(humminbird_trk_point_old_t), th.num_points-1, fin)) {
     fatal(MYNAME ": Unexpected end of file reading points!\n");
   }
@@ -492,8 +481,8 @@ HumminbirdBase::humminbird_read_track_old(gbfile* fin)
 
   gbfseek(fin, file_len-TRK_NAME_LEN, SEEK_SET);
   gbfread(&namebuf, 1, TRK_NAME_LEN, fin);
-  trk->rte_name = xstrndup(namebuf, sizeof(namebuf));
 
+  trk->rte_name = QByteArray(namebuf, static_cast<int>(qstrnlen(namebuf, sizeof(namebuf))));
   trk->rte_num  = th.trk_num;
 
   /* We create one wpt for the info in the header */
@@ -547,16 +536,14 @@ HumminbirdBase::humminbird_read_track_old(gbfile* fin)
     }
     track_add_wpt(trk, wpt);
   }
-  xfree(points);
+  delete[] points;
 }
 
 void
 HumminbirdBase::humminbird_read()
 {
   while (! gbfeof(fin_)) {
-    uint32_t signature = gbfgetuint32(fin_);
-
-    switch (signature) {
+    switch (uint32_t signature = gbfgetuint32(fin_)) {
     case WPT_MAGIC:
     case WPT_MAGIC2:
       humminbird_read_wpt(fin_);
@@ -631,8 +618,8 @@ HumminbirdBase::humminbird_wr_deinit()
 void
 HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt)
 {
-  humminbird_waypt_t hum;
-  int num_icons = sizeof(humminbird_icons) / sizeof(humminbird_icons[0]);
+  humminbird_waypt_t hum{0};
+  int num_icons = std::size(humminbird_icons);
 
   be_write16(&hum.num, waypoint_num++);
   hum.zero   = 0;
@@ -688,9 +675,8 @@ HumminbirdHTFormat::humminbird_track_head(const route_head* trk)
   trk_head = nullptr;
   last_time = 0;
   if (!trk->rte_waypt_empty()) {
-    trk_head = (humminbird_trk_header_t*) xcalloc(1, sizeof(humminbird_trk_header_t));
-    trk_points = (humminbird_trk_point_t*) xcalloc(max_points, sizeof(humminbird_trk_point_t));
-
+    trk_head = new humminbird_trk_header_t();
+    trk_points = new humminbird_trk_point_t[max_points]();
     QString name = trkname_sh->mkshort(trk->rte_name);
     strncpy(trk_head->name, CSTR(name), sizeof(trk_head->name)-1);
     be_write16(&trk_head->trk_num, trk->rte_num);
@@ -727,8 +713,8 @@ HumminbirdHTFormat::humminbird_track_tail(const route_head* /*unused*/)
   gbfwrite(trk_points, max_points, sizeof(humminbird_trk_point_t), fout_);
   gbfputuint16(0, fout_); /* Odd but true. The format doesn't fit an int nr of entries. */
 
-  xfree(trk_head);
-  xfree(trk_points);
+  delete trk_head;
+  delete[] trk_points;
 
   trk_head   = nullptr;
   trk_points = nullptr;
@@ -768,8 +754,8 @@ HumminbirdHTFormat::humminbird_track_cb(const Waypoint* wpt)
   } else {
     /* These points are 16-bit differential. */
     int j = i-1;
-    trk_points[j].deltaeast = east - last_east;
-    trk_points[j].deltanorth = north - last_north;
+    trk_points[j].deltaeast = static_cast<int16_t>(east - last_east);
+    trk_points[j].deltanorth =  static_cast<int16_t>(north - last_north);
     trk_points[j].depth = qRound(wpt->depth_value_or(0) * 100.0);
 
     /* BE-ify */
@@ -819,7 +805,7 @@ HumminbirdFormat::humminbird_rte_head(const route_head* rte)
 {
   humrte = nullptr;
   if (!rte->rte_waypt_empty()) {
-    humrte = (humminbird_rte_t*) xcalloc(1, sizeof(*humrte));
+    humrte = new humminbird_rte_t;
   }
 }
 
@@ -847,7 +833,7 @@ HumminbirdFormat::humminbird_rte_tail(const route_head* rte)
     gbfwrite(humrte, sizeof(*humrte), 1, fout_);
   }
 
-  xfree(humrte);
+  delete humrte;
   humrte = nullptr;
 }